﻿using NB.MGJ.Model;
using NB.MGJ.Model.CodeManage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public partial class ProductController 
    {


        //----------------------------------------产品start---------------------------------//

        #region 产品视图

        public ActionResult ProductList()
        {
            ViewData["TypeId"] = producttypedrop(0);
            return View();
        }

        public ActionResult ProductAdd_V()
        {
            var enterpriseModel = _ManagerService.GetEnterpriseModel_Eid(LogOnUser.EnterpriseId);
            var fatherId = enterpriseModel.ClassifyId;//父ID从机构行业中取
            if (fatherId == "1")
            {
             fatherId="0";
            }

           

            ViewData["TypeId"] = _productService.GetProductTypeSelectList(0, LogOnUser.EnterpriseId);
            ViewData["ClassifyId"] = _productService.GetClassifySelectList(0,fatherId);



            ViewBag.eid = LogOnUser.EnterpriseId;
            return View();
        }

        public ActionResult ProductEdit_V(int pid)
        {

            var enterpriseModel = _ManagerService.GetEnterpriseModel_Eid(LogOnUser.EnterpriseId);
            var fatherId = enterpriseModel.ClassifyId;//父ID从机构行业中取
            Product_Product model = _productService.GetProModel(pid);
            ViewData["TypeId"] = _productService.GetProductTypeSelectList(Convert.ToInt32(model.TypeId), 
                LogOnUser.EnterpriseId);

            ViewData["ClassifyId"] = _productService.GetClassifySelectList(Convert.ToInt32(model.ClassifyId), fatherId);
            ViewBag.Product = model;
            ViewBag.eid = LogOnUser.EnterpriseId;
            return View();
        }

        #endregion

        #region 增
        public int saveProduct(L.EntityModel.Product_Product model, string TransitionPath)
        {

            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.CreateDate = DateTime.Now;
            model.CreateLoginID = LogOnUser.UserID;
            model.CreateLoginName = LogOnUser.LoginName;
            int result = 0;


            //添加
            if (model.Id == 0)
            {
                model.TotalInventory = 0;               
                model.ViewDel = 0;
                model.SaleStatus = 0;
                result = _productService.AddProduct(model);

                if (Directory.Exists(Server.MapPath(TransitionPath)))
                {

                    FileInfo file = new FileInfo(TransitionPath);
                    string newFname = TransitionPath.Replace(TransitionPath.Split('/')[4], result.ToString());
                    Directory.Move(Server.MapPath(TransitionPath), Server.MapPath(newFname));
                    L.EntityModel.Product_Product p = new L.EntityModel.Product_Product();
                    p.Id = result;
                    //去图片名字
                    p.ImageUrl1 = model.ImageUrl1 != null ? newFname + "/" + model.ImageUrl1.Split('/')[model.ImageUrl1.Split('/').Length - 1] : "";
                    p.ImageUrl2 = model.ImageUrl2 != null ? newFname + "/" + model.ImageUrl2.Split('/')[model.ImageUrl2.Split('/').Length - 1] : "";
                    p.ImageUrl3 = model.ImageUrl3 != null ? newFname + "/" + model.ImageUrl3.Split('/')[model.ImageUrl3.Split('/').Length - 1] : "";
                    p.ImageUrl4 = model.ImageUrl4 != null ? newFname + "/" + model.ImageUrl4.Split('/')[model.ImageUrl4.Split('/').Length - 1] : "";
                    p.ImageUrl5 = model.ImageUrl5 != null ? newFname + "/" + model.ImageUrl5.Split('/')[model.ImageUrl5.Split('/').Length - 1] : "";
                    p.ChangeDate = model.ChangeDate;

                    _productService.UpdProduct(p);


                }
            }
            else
            {

                result = _productService.UpdProduct(model);
            }

            return result;
        }



        #endregion

        #region 删

        public int DelProduct(int id)
        {
            L.EntityModel.Product_Product model = new L.EntityModel.Product_Product();
            model.Id = id;
            model.ViewDel = 1;
            int result = _productService.UpdProduct(model);
            return result;
        }

        public ActionResult DeleteProduct(int id)
        {
            _productService.DelteProduct(id);
            return Json(true);
        }



        #endregion

        #region 改

        //和新增共用一个接口

        #endregion

        #region 查

        public ActionResult GetProductList( DetailSearch Smodel,int pageIndex=1, int pageSize=10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            var result = _productService.GetProductList(Smodel, ref pm, LogOnUser);
            return Json(result);
        }
        //产品类型下拉框
        public List<SelectListItem> producttypedrop(int TypeId)
        {
            List<Product_ProductTypes> result = _productService.GetprotypeService(LogOnUser.EnterpriseId);
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "--请选择--", Value = "" });
            for (int i = 0; i < result.Count; i++)
            {
                SelectListItem item = new SelectListItem() { Text = result[i].TypeName, Value = result[i].Id.ToString() };
                if (TypeId == result[i].Id)
                {

                    item.Selected = true;
                }
                items.Add(item);
            }
            return items;
        }
		
        public ActionResult GetProductModelById(int productId)
        {
            var model = _productService.GetProModel(productId);
            return Json(model);
        }

        #endregion


	}
}